<template>
  <div class="login">
    <div class="login-box">
      <img src="../assets/logo.png" />
      <el-form :model="loginForm" ref="loginRef" :rules="loginRule">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名">
            <i slot="prefix" class="iconfont icon-user"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            placeholder="请输入密码"
            type="password"
          >
            <i slot="prefix" class="iconfont icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "1234567"
      },
      loginRule: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 2, max: 7, message: "用户名长度为2-7个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 7, max: 14, message: "密码长度为7-14个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    login() {
      this.$refs.loginRef.validate(valid => {
        if (valid) {
          this.$router.push("/");
        } else {
          return false;
        }
      });
    },
    reset() {
      this.$refs.loginRef.resetFields();
    }
  }
};
</script>

<style lang="scss" scoped>
.login {
  height: 100vh;
  background-color: #2b4b6b;
}
.login-box {
  width: 600px;
  height: 400px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  img {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    border: 10px solid #fff;
    background-color: #eee;
    box-shadow: 0 0 10px #eee;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .el-form {
    width: 90%;
    margin: 0 auto;
    margin-top: 150px;
    .el-form-item:nth-child(3) {
      text-align: center;
      margin-top: 50px;
    }
  }
}
</style>
